package bb.lanxing.calc.calculator;

import com.github.mikephil.charting.utils.Utils;

public class GeoidHeightCalc {
    float[][] calibration = {
            new float[]{
                    -30.0f, -30.0f, -30.0f, -30.0f, -30.0f, -30.0f, -30.0f, -30.0f, -30.0f, -30.0f, -30.0f, -30.0f,
                    -30.0f, -30.0f, -30.0f, -30.0f, -30.0f, -30.0f, -30.0f, -30.0f, -30.0f, -30.0f, -30.0f, -30.0f,
                    -30.0f, -30.0f, -30.0f, -30.0f, -30.0f, -30.0f, -30.0f, -30.0f, -30.0f, -30.0f, -30.0f, -30.0f, -30.0f},
            new float[]{
                    -4.0f, -1.0f, 1.0f, 4.0f, 4.0f, 6.0f, 5.0f, 4.0f, 2.0f, -6.0f, -15.0f, -24.0f, -33.0f, -40.0f,
                    -48.0f, -50.0f, -53.0f, -52.0f, -53.0f, -54.0f, -55.0f, -52.0f, -48.0f, -42.0f, -38.0f, -38.0f,
                    -29.0f, -26.0f, -26.0f, -24.0f, -23.0f, -21.0f, -19.0f, -16.0f, -12.0f, -8.0f, -4.0f},
            new float[]{
                    16.0f, 16.0f, 17.0f, 21.0f, 20.0f, 26.0f, 26.0f, 22.0f, 16.0f, 10.0f, -1.0f, -16.0f, -29.0f,
                    -36.0f, -46.0f, -55.0f, -54.0f, -59.0f, -61.0f, -60.0f, -61.0f, -55.0f, -49.0f, -44.0f, -38.0f,
                    -31.0f, -25.0f, -16.0f, -6.0f, 1.0f, 4.0f, 5.0f, 4.0f, 2.0f, 6.0f, 12.0f, 16.0f},
            new float[]{
                    16.0f, 19.0f, 25.0f, 30.0f, 35.0f, 33.0f, 35.0f, 30.0f, 27.0f, 10.0f, -2.0f, -14.0f, -23.0f,
                    -30.0f, -33.0f, -29.0f, -35.0f, -43.0f, -45.0f, -43.0f, -37.0f, -32.0f, -30.0f, -25.0f, -23.0f,
                    -22.0f, -16.0f, -10.0f, -2.0f, 10.0f, 20.0f, 20.0f, 21.0f, 24.0f, 22.0f, 17.0f, 16.0f},
            new float[]{
                    25.0f, 26.0f, 34.0f, 39.0f, 45.0f, 45.0f, 38.0f, 39.0f, 28.0f, 13.0f, -1.0f, -15.0f, -22.0f,
                    -22.0f, -18.0f, -15.0f, -14.0f, -10.0f, -15.0f, -18.0f, -18.0f, -16.0f, -17.0f, -15.0f, -10.0f,
                    -10.0f, -8.0f, -2.0f, 6.0f, 14.0f, 13.0f, 3.0f, 3.0f, 10.0f, 20.0f, 27.0f, 25.0f},
            new float[]{
                    18.0f, 26.0f, 31.0f, 33.0f, 39.0f, 41.0f, 30.0f, 24.0f, 13.0f, -2.0f, -20.0f, -32.0f, -33.0f,
                    -27.0f, -14.0f, -2.0f, 5.0f, 20.0f, 21.0f, 6.0f, 1.0f, -7.0f, -12.0f, -12.0f, -12.0f, -10.0f,
                    -7.0f, -1.0f, 8.0f, 23.0f, 15.0f, -2.0f, -6.0f, 6.0f, 21.0f, 24.0f, 18.0f},
            new float[]{
                    22.0f, 27.0f, 34.0f, 29.0f, 14.0f, 15.0f, 15.0f, 7.0f, -9.0f, -25.0f, -37.0f, -39.0f,
                    -23.0f, -14.0f, 15.0f, 33.0f, 34.0f, 45.0f, 46.0f, 22.0f, 5.0f, -2.0f, -8.0f, -13.0f, -10.0f,
                    -7.0f, -4.0f, 1.0f, 9.0f, 32.0f, 16.0f, 4.0f, -8.0f, 4.0f, 12.0f, 15.0f, 22.0f},
            new float[]{
                    17.0f, 23.0f, 21.0f, 8.0f, -9.0f, -10.0f, -11.0f, -20.0f, -40.0f, -47.0f, -45.0f, -25.0f,
                    5.0f, 23.0f, 45.0f, 58.0f, 57.0f, 63.0f, 51.0f, 27.0f, 10.0f, 0.0f, -9.0f, -11.0f, -5.0f,
                    -2.0f, -3.0f, -1.0f, 9.0f, 35.0f, 20.0f, -5.0f, -6.0f, -5.0f, 0.0f, 13.0f, 17.0f},
            new float[]{
                    12.0f, 13.0f, -2.0f, -14.0f, -25.0f, -32.0f, -38.0f, -60.0f, -75.0f, -63.0f, -26.0f, 0.0f,
                    35.0f, 52.0f, 68.0f, 76.0f, 64.0f, 52.0f, 36.0f, 22.0f, 11.0f, 6.0f, -1.0f, -8.0f, -10.0f,
                    -8.0f, -11.0f, -9.0f, 1.0f, 32.0f, 4.0f, -18.0f, -13.0f, -9.0f, 4.0f, 14.0f, 12.0f},
            new float[]{
                    18.0f, 12.0f, -13.0f, -9.0f, -28.0f, -49.0f, -62.0f, -89.0f, -102.0f, -63.0f, -9.0f, 33.0f,
                    58.0f, 73.0f, 74.0f, 63.0f, 50.0f, 32.0f, 22.0f, 16.0f, 17.0f, 13.0f, 1.0f, -12.0f, -23.0f,
                    -20.0f, -14.0f, -3.0f, 14.0f, 10.0f, -15.0f, -27.0f, -18.0f, 3.0f, 12.0f, 20.0f, 18.0f},
            new float[]{
                    22.0f, 23.0f, 2.0f, -3.0f, -7.0f, -36.0f, -59.0f, -90.0f, -95.0f, -63.0f, -24.0f, 12.0f,
                    53.0f, 60.0f, 58.0f, 46.0f, 36.0f, 26.0f, 13.0f, 12.0f, 11.0f, 2.0f, -11.0f, -28.0f, -38.0f,
                    -29.0f, -10.0f, 3.0f, 1.0f, -11.0f, -41.0f, -42.0f, -16.0f, 3.0f, 17.0f, 33.0f, 22.0f},
            new float[]{
                    31.0f, 26.0f, 15.0f, 6.0f, 1.0f, -29.0f, -44.0f, -61.0f, -67.0f, -59.0f, -36.0f, -11.0f,
                    21.0f, 39.0f, 49.0f, 39.0f, 22.0f, 10.0f, 5.0f, 10.0f, 7.0f, -7.0f, -23.0f, -39.0f, -47.0f,
                    -34.0f, -9.0f, -10.0f, -20.0f, -45.0f, -48.0f, -32.0f, -9.0f, 17.0f, 25.0f, 31.0f, 31.0f},
            new float[]{
                    36.0f, 28.0f, 29.0f, 17.0f, 12.0f, -20.0f, -15.0f, -40.0f, -33.0f, -34.0f, -34.0f, -28.0f,
                    7.0f, 29.0f, 43.0f, 20.0f, 4.0f, -6.0f, -7.0f, -5.0f, -8.0f, -15.0f, -28.0f, -40.0f, -42.0f,
                    -29.0f, -22.0f, -26.0f, -32.0f, -51.0f, -40.0f, -17.0f, 17.0f, 31.0f, 34.0f, 44.0f, 36.0f},
            new float[]{
                    52.0f, 48.0f, 35.0f, 40.0f, 33.0f, -9.0f, -28.0f, -39.0f, -48.0f, -59.0f, -50.0f, -28.0f,
                    3.0f, 23.0f, 37.0f, 18.0f, -1.0f, -11.0f, -12.0f, -10.0f, -13.0f, -20.0f, -31.0f, -34.0f,
                    -21.0f, -16.0f, -26.0f, -34.0f, -33.0f, -35.0f, -26.0f, 2.0f, 33.0f, 59.0f, 52.0f, 51.0f, 52.0f},
            new float[]{
                    47.0f, 48.0f, 42.0f, 28.0f, 12.0f, -10.0f, -19.0f, -33.0f, -43.0f, -42.0f, -43.0f, -29.0f,
                    -2.0f, 17.0f, 23.0f, 22.0f, 6.0f, 2.0f, -8.0f, 8.0f, 8.0f, 1.0f, -11.0f, -19.0f, -16.0f,
                    -18.0f, -22.0f, -35.0f, -40.0f, -26.0f, -12.0f, 24.0f, 45.0f, 63.0f, 62.0f, 59.0f, 47.0f},
            new float[]{
                    47.0f, 41.0f, 21.0f, 18.0f, 14.0f, 7.0f, -3.0f, -22.0f, -29.0f, -32.0f, -32.0f, -26.0f,
                    -15.0f, -2.0f, 13.0f, 17.0f, 19.0f, 6.0f, 2.0f, 9.0f, 17.0f, 10.0f, 13.0f, 1.0f, -14.0f,
                    -30.0f, -39.0f, -46.0f, -42.0f, -21.0f, 6.0f, 29.0f, 49.0f, 65.0f, 60.0f, 57.0f, 47.0f},
            new float[]{
                    51.0f, 43.0f, 29.0f, 20.0f, 12.0f, 5.0f, -2.0f, -10.0f, -14.0f, -12.0f, -10.0f, -14.0f,
                    -12.0f, -6.0f, -2.0f, 3.0f, 6.0f, 4.0f, 2.0f, 2.0f, 1.0f, -1.0f, -3.0f, -7.0f, -14.0f,
                    -24.0f, -27.0f, -25.0f, -19.0f, 3.0f, 24.0f, 37.0f, 47.0f, 60.0f, 61.0f, 58.0f, 51.0f},
            new float[]{
                    33.0f, 34.0f, 28.0f, 23.0f, 17.0f, 13.0f, 9.0f, 4.0f, 4.0f, 1.0f, -2.0f, -2.0f, 0.0f, 2.0f,
                    3.0f, 2.0f, 1.0f, 1.0f, 3.0f, 1.0f, -2.0f, -3.0f, -3.0f, -3.0f, -1.0f, 3.0f, 1.0f, 5.0f,
                    9.0f, 11.0f, 19.0f, 27.0f, 31.0f, 34.0f, 33.0f, 34.0f, 33.0f},
            new float[]{
                    13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f,
                    13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f,
                    13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f},
            new float[]{
                    13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f,
                    13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f,
                    13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f, 13.0f}
    };

    private float adjust(float f, float f2, float f3, float f4, float f5) {
        if (Math.abs(f4 - f3) < 1.0E-6d) {
            return f;
        }
        if (f3 > f4) {
            f4 = f3;
            f3 = f4;
        } else {
            f2 = f;
            f = f2;
        }
        return (((f5 - f3) / (f4 - f3)) * (f - f2)) + f2;
    }

    private float adjust(float f, float f2, float f3, float f4, float f5, float f6, float f7, float f8, float f9, float f10, float f11, float f12, float f13, float f14) {
        return adjust(adjust(f3, f6, f, f4, f13), adjust(f9, f12, f7, f10, f13), f2, f8, f14);
    }

    public float adjust(double d, double d2) {
        double d3 = d2 > 180.0d ? d2 - 360.0d : d2;
        double d4 = (d <= 90.0d || d >= 90.0010000000475d) ? d : 90.0d;
        if (d4 < -90.0d && d4 > -90.0010000000475d) {
            d4 = -90.0d;
        }
        if (d3 > 180.0d && d3 < 180.0010000000475d) {
            d3 = 180.0d;
        }
        if (d3 < -180.0d && d3 > -180.0010000000475d) {
            d3 = -180.0d;
        }
        if (d3 < -180.0d || d3 > 180.0d) {
            return 0.0f;
        }
        if (d4 < -90.0d || d4 > 90.0d) {
            return 0.0f;
        }
        if (d3 < Utils.DOUBLE_EPSILON) {
            d3 += 360.0d;
        }
        double d5 = d3 / 10.0d;
        double d6 = (d4 + 90.0d) / 10.0d;
        int floor = (int) Math.floor(d5);
        int floor2 = (int) Math.floor(d6);
        float f = floor;
        float f2 = floor2;
//        float[][] fArr = this.a;
        float f3 = f + 1.0f;
        int i = floor + 1;
        float f4 = f2 + 1.0f;
        int i2 = floor2 + 1;
        return adjust(f, f2, calibration[floor2][floor], f3, f2, calibration[floor2][i], f, f4, calibration[i2][floor], f3, f4, calibration[i2][i], (float) d5, (float) d6);
    }
}